<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@page import="java.sql.Connection"%>
<%@page import="com.yuxin.common.util.DBUtil"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="com.yuxin.common.util.CommonUtil"%>
<%@page import="com.yuxin.core.TransactionManager"%>
<%@page import="java.util.Date"%>
<%
	Connection conn = null;
	TransactionManager tranM = null;
	String id = request.getParameter("id");
	String action = request.getParameter("action");
	Map<String, Object> orderMap = null;
	String redirect = null;
	try {
		if(!CommonUtil.isLoginValid(request)){
			response.sendRedirect(request.getContextPath()+"/site/denglu.jsp");
			return;
		 }
		conn = DBUtil.getConnection();
		 // 页面标题
        request.setAttribute("titleName",URLDecoder.decode("订单付款", "UTF-8"));
		
		String sql = "select t.ID, t.CODE, t.TOTAL_PRICE, DATE_FORMAT(t.CREATE_TIME, '%Y-%m-%d %H:%i') as CREATETIME from p_orders t" 
					 + " where t.ID = ?";
		orderMap = DBUtil.queryUniqueResult(conn, sql, id);
		
		if(orderMap == null || orderMap.isEmpty()) {
			response.sendRedirect(request.getContextPath()+"/site/my_order.jsp");
			return;
		}
		
		if(action != null && "pay".equals(action)) {
			tranM = new TransactionManager(conn);
			tranM.begin();
			sql= "update p_orders set STATUS = 2, PAY_TIME = ? where id = ?";
			int res = DBUtil.executeSql(conn, sql, new Object[]{new Date(), id});
			if(res > 0) {
				redirect = request.getContextPath()+"/site/pay.jsp?id="+id+"&res_code=101";
			}
		}
	} catch (Exception ex) {
		if(tranM != null) {
			tranM.rollback();
		}
		redirect = request.getContextPath()+"/site/pay.jsp?id="+id+"&res_code=102";
		ex.printStackTrace();
	} finally {
		if(tranM != null) {
			tranM.commit();
		}
		DBUtil.close(conn);
		if(redirect != null) {
			response.sendRedirect(redirect);
		}
	}
%>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>${titleName}</title>
        <meta name="Keywords" content="中信信用卡平台" />
        <meta name="Description" content="${titleName}"/>
        <meta name="robots" content="all"/>
        <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes"/>
        <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
        <meta name="format-detection" content="telephone=no"/>
        <meta http-equiv="cleartype" content="on"/>
        <link rel="icon" type="image/png" href="images/favicon.ico">
        <%@ include file="/site/css.jsp" %>
        <style type="text/css">
			.biaoge{margin-top:10px;width:100%;background-color:#fff;overflow:hidden;border:1px solid #C6C4C2;line-height:24px;-webkit-border-radius: .2em;-moz-border-radius: .2em;border-radius: .2em;}
			.biaoge li{list-style-type: none;color:#666;padding:2px 10px;border-bottom:1px solid #C6C4C2;line-height:30px;overflow:hidden;}
			.biaoge li:last-child{border-bottom: none;}
			.biaoge .name{float:left;}
			.biaoge .value{float:right;}
		</style>
	</head>
	<body>
        <div id="page">
            <%@ include file="/site/header_nav.jsp" %>
            <div id="container">
            	<form id="payForm" name="payForm" method="post" action="pay.jsp?id=${param.id}&amp;action=pay">
            	<ul class="biaoge">
                	<li><div class="name">订单编号</div><div class="value"><%=orderMap.get("CODE") %></div></li>
                	<li><div class="name">订单金额</div><div class="value"><%=orderMap.get("TOTAL_PRICE") %>元</div></li>
                    <li><div>支付密码</div><div><input id="pay_pwd" name="pwd_pwd" value="" type="password" class="text" style="width:95%" placeholder="请输入支付密码" /></div></li>
                    <li><div><a href="#" class="button big blue" onClick="doOK();return false;">确定</a></div></li>
                </ul>             
                </form>
            </div>
            <%@ include file="/site/footer.jsp" %>
        </div>
    </body>
    <%@ include file="/site/js.jsp" %>
	<script src="${ctx}/site/js/jquery.form.js"></script>
    <script>
		$(document).ready(function(e) {
            var res_code = "${param.res_code}";
			if(res_code != '') {
				if(res_code == "101") {
					appUtil.alert("支付成功！");									
				} else if(res_code == "102") {
					appUtil.alert("支付失败！");
				}	
				setTimeout("window.location.href='${ctx}/site/my_order.jsp';",3000);			
			}
        });
        function doOK(){
            if($("#pay_pwd").val().length < 1){
                appUtil.alert("请输入支付密码！");
                $("#pay_pwd").focus();
                return;
            }
            $('#payForm').submit();
        }
    </script>
</html>